//
//  SigninAccountViewController.m
//  Btpin
//
//  Created by Vic on 2019/3/11.
//  Copyright © 2019 Vic. All rights reserved.
//

#import "SigninAccountViewController.h"
#import "FindPwdViewController.h"
//#import "ResignAgreementViewController.h"
#import "GeneralWebViewViewController.h"

@interface SigninAccountViewController ()<UITextFieldDelegate>
{
    UITextField *phoneTextField;
    UITextField *passwordTextField;
}
@end

@implementation SigninAccountViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self initUI];
}
- (void)initUI {
    self.navigationItem.title = @"账号登录";
    
    self.leftButton = [UIButton buttonWithType:UIButtonTypeCustom];
    self.leftButton.frame = CGRectMake(0, 0, 44, 44);
//    self.leftButton.imageEdgeInsets = UIEdgeInsetsMake(11, 0, 12, 32);
    [self.leftButton setImage:[UIImage imageNamed:@"back_b"] forState:UIControlStateNormal];
    [self.leftButton addTarget:self action:@selector(customBackButtonTap) forControlEvents:UIControlEventTouchUpInside];
    
    UIBarButtonItem *backItem = [[UIBarButtonItem alloc] initWithCustomView:self.leftButton];
    self.navigationItem.leftBarButtonItem = backItem;
    
    
    phoneTextField = [UIToolsControl initTextFieldWithFrame:CGRectMake(30*Scale, 25*Scale, WIDTH-60*Scale, 50*Scale) placeholder:PhoneNumberPlaceholderText secureTextEntry:NO font:Font_17 padding:YES];
    [phoneTextField borderColor:THEMECOLOR_OF_TEXT_cccccc borderWidth:lineH borderType:BorderSideTypeBottom];
    phoneTextField.delegate = self;
    phoneTextField.keyboardType = UIKeyboardTypeNumberPad;
    [self.view addSubview:phoneTextField];
    
    passwordTextField = [UIToolsControl initTextFieldWithFrame:CGRectMake(30*Scale, EndY(phoneTextField)+25*Scale, WIDTH-60*Scale, 50*Scale) placeholder:PasswordPlaceholderText secureTextEntry:YES font:Font_17 padding:YES];
    [passwordTextField borderColor:THEMECOLOR_OF_TEXT_cccccc borderWidth:lineH borderType:BorderSideTypeBottom];
    passwordTextField.delegate = self;
    passwordTextField.keyboardType = UIKeyboardTypeNamePhonePad;
    [self.view addSubview:passwordTextField];
    
    UIButton *forgetPwdBtn = [UIToolsControl initButtonWithFrame:CGRectMake(WIDTH-100*Scale, EndY(passwordTextField), 80*Scale, 40*Scale) imageName:nil target:self action:@selector(forgetPwdBtnTap) title:@"忘记密码?" titleColor:THEMECOLOR_OF_TEXT_333333];
    forgetPwdBtn.titleLabel.font = Font(Font_14);
    [self.view addSubview:forgetPwdBtn];
    
    
    UIButton *okBtn = [UIToolsControl initButtonWithFrame:CGRectMake(40*Scale, EndY(passwordTextField)+70*Scale, WIDTH-80*Scale, 46*Scale) imageName:nil target:self action:@selector(okBtnTap) title:@"登录" titleColor:COLOR_OF_WHITE];
    okBtn.titleLabel.font = FontBold(Font_17);
    okBtn.backgroundColor = THEMECOLOR_OF_ORANGE;
    okBtn.layer.cornerRadius = 23*Scale;
    okBtn.layer.masksToBounds = YES;
    [self.view addSubview:okBtn];
    
    NSString *clauseString = @"进入比特聘 表示您同意《比特聘网协议》";
    NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:clauseString];
    [attributedString addAttribute:NSFontAttributeName value:Font(Font_13) range:NSMakeRange(0, clauseString.length)];
    [attributedString addAttribute:NSForegroundColorAttributeName value:THEMECOLOR_OF_TEXT_666666 range:NSMakeRange(0, clauseString.length)];
    [attributedString addAttribute:NSForegroundColorAttributeName value:THEMECOLOR_OF_TEXT_333333 range:NSMakeRange(11, 8)];
    UILabel *clauseLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, EndY(okBtn)+5*Scale, WIDTH, 30*Scale)];
    clauseLabel.font = Font(Font_13);
    clauseLabel.backgroundColor = COLOR_OF_CLEAR;
    clauseLabel.attributedText = attributedString;
    clauseLabel.textAlignment = NSTextAlignmentCenter;
    [self.view addSubview:clauseLabel];
    __weak __typeof(self)weakSelf = self;
    [clauseLabel yb_addAttributeTapActionWithStrings:@[@"比特聘网协议"] tapClicked:^(NSString *string, NSRange range, NSInteger index) {
        NSLog(@"比特聘网协议");
        __strong __typeof(weakSelf)strongSelf = weakSelf;
        GeneralWebViewViewController *GWVC = [[GeneralWebViewViewController alloc] init];
        GWVC.showType = GeneralWebViewTypeOfResignAgreement;
        GWVC.hidesBottomBarWhenPushed = YES;
        [strongSelf.navigationController pushViewController:GWVC animated:YES];
    }];
    
    UIButton *captchaBtn = [UIToolsControl initButtonWithFrame:CGRectMake(20*Scale, HEIGHT-HeightOfStatusBarAndNavigationBar-SIMEmojKeyBoardkBottomNOSafeArea_Height-70*Scale, 160*Scale, 40*Scale) imageName:nil target:self action:@selector(captchaBtnTap) title:@"手机验证码注册/登录" titleColor:THEMECOLOR_OF_TEXT_333333];
    captchaBtn.titleLabel.font = Font(Font_15);
    [self.view addSubview:captchaBtn];
}
- (void)okBtnTap {
    if (phoneTextField.text.length>0) {
        if ([UIToolsControl validatePhoneNumber:phoneTextField.text]) {
            if (passwordTextField.text.length>0) {
                if ([UIToolsControl validateNumberAndLetter:passwordTextField.text]) {
                    
                } else {
                    SVPHUDError(PasswordOKAlertText);
                    [passwordTextField becomeFirstResponder];
                }
            } else {
                SVPHUDError(PasswordAlertText);
                [passwordTextField becomeFirstResponder];
            }
        } else {
            SVPHUDError(PhoneNumberOKAlertText);
            [phoneTextField becomeFirstResponder];
        }
    } else {
        SVPHUDError(PhoneNumberAlertText);
        [phoneTextField becomeFirstResponder];
    }
}
- (void)captchaBtnTap {
    NSLog(@"captchaBtnTap");
    self.signin(NO);
    [self.navigationController popViewControllerAnimated:YES];
}
- (void)forgetPwdBtnTap {
    NSLog(@"forgetPwdBtnTap");
    FindPwdViewController *FPVC = [[FindPwdViewController alloc] init];
    [self.navigationController pushViewController:FPVC animated:YES];
}
- (void)customBackButtonTap {
    self.signin(NO);
    [self.navigationController popViewControllerAnimated:YES];
}

@end
